home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7315 < prev    next >
Encoding:
Text File  |  1996-08-05  |  4.8 KB  |  131 lines

  1. Path: newshost.lanl.gov!tanmoy
  2. From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Confusion as to the proper use of MODULAS
  5. Date: 15 Feb 1996 00:26:28 GMT
  6. Organization: Los Alamos National Laboratory
  7. Message-ID: <TANMOY.96Feb14172628@qcd.lanl.gov>
  8. References: <4fr8be$ass@news.iconn.net> <31224679.6193@born.com>
  9.     <4fthsu$1kl@ixnews7.ix.netcom.com>
  10. NNTP-Posting-Host: qcd.lanl.gov
  11. Mime-Version: 1.0
  12. Content-Type: text
  13. In-reply-to: wzjn@ix.netcom.com's message of 14 Feb 1996 20:49:34 GMT
  14.  
  15. In article <4fthsu$1kl@ixnews7.ix.netcom.com> wzjn@ix.netcom.com (KPN
  16. ) writes: 
  17. <snip>
  18.    Confusion as to the proper use of MODULAS
  19.  
  20. That is a funny word. I know of modulus ... guess you must be talking
  21. about the same concept :-) More commonly, the result of % is called
  22. the remainder, and is the basis of a branch of arithmetic called
  23. modular arithmetic.
  24.  
  25. <snip>
  26.    Modulas operator simply shows:  *** AFTER *** you divide, what is the
  27.    remainder?
  28.  
  29. Well it couldn't be the remainder, before you divide, could it be?
  30. After all before you can think of something remaining, you have to do
  31. something, right? So, yes, the result of x % y is what remains of x
  32. when you have taken out as many y's as you could out of it. The
  33. precise meaning is in terms of division: it says, that you divide x by
  34. y to get an integral value, multiply the result back by y and subtract
  35. from x. Thus x % y = x - (x/y)*y
  36.  
  37.    EXAMPLE of this:
  38.        9 % 7 = ?
  39.        9 divided by 7 yields a remainder of 2
  40.  
  41. Another way of looking at it is that you can take one 7 out of 9, and
  42. after taking it away, you have 2 left over. You cannot take out any
  43. more 7's, so the result is 7.
  44.  
  45. Of course 9/7 is 1 by the same count. So, 9 % 7 = 9-(9/7)*7 = 2.
  46.  
  47.        9 % 7 = 2
  48.  
  49.    1) MODULAS means divide a number and give me the left over number. OK -
  50.    not bad.
  51.  
  52. Correct. This is always the meaning. x % y = x - (x/y)*y, always (or
  53. rather when x/y is makes sense. Aside for experts: the standard seems
  54. to require no overflow on the rhs ... I had not expected this: maybe I
  55. am misreading the standard in my hurry to finish this post.)
  56.  
  57.    Next rule:
  58.    If the number to be divided is smaller than the number to divide by,
  59.    the result will always be the number being divided
  60.  
  61. This is a subcase of the previous rule. If you cannot take any y's out
  62. of x, then what remains is the whole of x, right?
  63.  
  64.    EXAMPLE of this:
  65.  
  66.       9 % 12 = ?
  67.       can not divide 9 by a larger number
  68.       9 % 12 = 9
  69.    Now I have rule #2
  70.  
  71. Yes. But see, you cannot take 12 out of 9: so, what is left over is
  72. 9. Doing it the other way, 9/12=0 (i.e. no 12's can be taken out of
  73. 9), so 9 % 12 = 9-(9/12)*12 = 9
  74.  
  75.    2) If the number to be divided is smaller than the nuber to be used as
  76.    a divisor, the result is the original number. OK - again, not bad.
  77.  
  78. Do you see that this rule is not really needed.
  79.  
  80.    Here, begin my troubles: using MODULAS in an IF statement.
  81.  
  82.    I made up a test to see if a number I inputted was a 7. Sample code:
  83.  
  84.       if (number % 7)
  85.      printf("Not a 7\n");
  86.       else
  87.      printf("First integer was a 7\n");
  88.  
  89.    So, if the number WAS a 7, the ELSE would take over. OK. But, when I
  90.    enter in a ZERO, it still tells me that the number was a seven?
  91.  
  92. Well, what does your second rule say? Isn't ZERO less that 7? In that
  93. case shouldn't the answer of number % 7 be number itself? And the
  94. number is ZERO.
  95.  
  96. Can you see that number % 7 is the same for number being 0, 7, 14, 21
  97. etc. 
  98.  
  99.    Am I going in the right direction? Can someone tell me what IÆm doing
  100.  
  101. I do not know. I have no idea where you are going ...
  102.  
  103.    incorrectly, or where IÆm straying? WhatÆs the rule here for using MOD
  104.    in an IF statement?
  105.  
  106. There is no IF statement, there is an if statement though. C is case
  107. sensitive, so you have to use the correct case. The only rule is that
  108. if the expression controlling the if is zero (of any kind, or a null
  109. pointer), the controlled statement is executed. If the value is
  110. non-zero (or a non-null pointer), the else branch, if any, is executed.
  111.  
  112.    Thank you for your help,
  113.    kevin
  114.  
  115. There is only one issue which you have to be careful about. If the
  116. numbers being divided are not positive, x % y = x - (x/y)*y is still
  117. correct, but the meaning of x/y may vary from one machine to another,
  118. and from one compiler to another. Thus (-3) / 2 can be either -1 or
  119. -2, but if (-3)/2 = -1, then (-3) % 2 is -1; whereas if (-3)/2 is -2,
  120. (-3) % 2 is +1. 
  121.  
  122. Cheers
  123. Tanmoy
  124. --
  125. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  126. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  127. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  128. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  129. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  130. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  131.